package androidx.appcompat.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.PorterDuff.Mode;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.ThemedSpinnerAdapter;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.d;
import androidx.core.view.ViewCompat;
import o.ah$j;
import o.ah.c;
import o.ah.g;
import o.ar;
import o.ax;
import o.bt;
import o.ca;
import o.cm;
import o.cr;
import o.cw;
import o.de;
import o.hy;

public class AppCompatSpinner extends Spinner implements hy {
    private static final int[] ˋ = new int[]{16843505};
    private final Context ʻ;
    private SpinnerAdapter ʼ;
    private cm ʽ;
    b ˊ;
    int ˎ;
    private final ca ˏ;
    final Rect ॱ;
    private final boolean ᐝ;

    class b extends ListPopupWindow {
        private CharSequence ʽ;
        ListAdapter ˊ;
        final /* synthetic */ AppCompatSpinner ˏ;
        private final Rect ᐝ = new Rect();

        public b(final AppCompatSpinner appCompatSpinner, Context context, AttributeSet attributeSet, int i) {
            this.ˏ = appCompatSpinner;
            super(context, attributeSet, i);
            ˏ((View) appCompatSpinner);
            ˎ(true);
            ˋ(0);
            ˊ(new OnItemClickListener(this) {
                final /* synthetic */ b ˊ;

                public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                    this.ˊ.ˏ.setSelection(i);
                    if (this.ˊ.ˏ.getOnItemClickListener() != null) {
                        this.ˊ.ˏ.performItemClick(view, i, this.ˊ.ˊ.getItemId(i));
                    }
                    this.ˊ.ˋ();
                }
            });
        }

        public void ˏ(ListAdapter listAdapter) {
            super.ˏ(listAdapter);
            this.ˊ = listAdapter;
        }

        public CharSequence ˏ() {
            return this.ʽ;
        }

        public void ˋ(CharSequence charSequence) {
            this.ʽ = charSequence;
        }

        void ॱ() {
            int i;
            int i2;
            Drawable ʼ = ʼ();
            if (ʼ != null) {
                ʼ.getPadding(this.ˏ.ॱ);
                i = de.ˎ(this.ˏ) ? this.ˏ.ॱ.right : -this.ˏ.ॱ.left;
            } else {
                Rect rect = this.ˏ.ॱ;
                this.ˏ.ॱ.right = 0;
                rect.left = 0;
                i = 0;
            }
            int paddingLeft = this.ˏ.getPaddingLeft();
            int paddingRight = this.ˏ.getPaddingRight();
            int width = this.ˏ.getWidth();
            if (this.ˏ.ˎ == -2) {
                int ˎ = this.ˏ.ˎ((SpinnerAdapter) this.ˊ, ʼ());
                i2 = (this.ˏ.getContext().getResources().getDisplayMetrics().widthPixels - this.ˏ.ॱ.left) - this.ˏ.ॱ.right;
                if (ˎ <= i2) {
                    i2 = ˎ;
                }
                ʼ(Math.max(i2, (width - paddingLeft) - paddingRight));
            } else if (this.ˏ.ˎ == -1) {
                ʼ((width - paddingLeft) - paddingRight);
            } else {
                ʼ(this.ˏ.ˎ);
            }
            if (de.ˎ(this.ˏ)) {
                i2 = ((width - paddingRight) - ॱˊ()) + i;
            } else {
                i2 = i + paddingLeft;
            }
            ˎ(i2);
        }

        public void ˊ() {
            boolean ˎ = ˎ();
            ॱ();
            ʻ(2);
            super.ˊ();
            ᐝ().setChoiceMode(1);
            ᐝ(this.ˏ.getSelectedItemPosition());
            if (!ˎ) {
                ViewTreeObserver viewTreeObserver = this.ˏ.getViewTreeObserver();
                if (viewTreeObserver != null) {
                    final OnGlobalLayoutListener anonymousClass4 = new OnGlobalLayoutListener(this) {
                        final /* synthetic */ b ˋ;

                        {
                            this.ˋ = r1;
                        }

                        public void onGlobalLayout() {
                            if (this.ˋ.ॱ(this.ˋ.ˏ)) {
                                this.ˋ.ॱ();
                                super.ˊ();
                                return;
                            }
                            this.ˋ.ˋ();
                        }
                    };
                    viewTreeObserver.addOnGlobalLayoutListener(anonymousClass4);
                    ॱ(new OnDismissListener(this) {
                        final /* synthetic */ b ˋ;

                        public void onDismiss() {
                            ViewTreeObserver viewTreeObserver = this.ˋ.ˏ.getViewTreeObserver();
                            if (viewTreeObserver != null) {
                                viewTreeObserver.removeGlobalOnLayoutListener(anonymousClass4);
                            }
                        }
                    });
                }
            }
        }

        boolean ॱ(View view) {
            return ViewCompat.ˋˊ(view) && view.getGlobalVisibleRect(this.ᐝ);
        }
    }

    static class e implements ListAdapter, SpinnerAdapter {
        private ListAdapter ˋ;
        private SpinnerAdapter ˏ;

        public e(@Nullable SpinnerAdapter spinnerAdapter, @Nullable Theme theme) {
            this.ˏ = spinnerAdapter;
            if (spinnerAdapter instanceof ListAdapter) {
                this.ˋ = (ListAdapter) spinnerAdapter;
            }
            if (theme == null) {
                return;
            }
            if (VERSION.SDK_INT >= 23 && (spinnerAdapter instanceof ThemedSpinnerAdapter)) {
                ThemedSpinnerAdapter themedSpinnerAdapter = (ThemedSpinnerAdapter) spinnerAdapter;
                if (themedSpinnerAdapter.getDropDownViewTheme() != theme) {
                    themedSpinnerAdapter.setDropDownViewTheme(theme);
                }
            } else if (spinnerAdapter instanceof cr) {
                cr crVar = (cr) spinnerAdapter;
                if (crVar.ˋ() == null) {
                    crVar.ˊ(theme);
                }
            }
        }

        public int getCount() {
            return this.ˏ == null ? 0 : this.ˏ.getCount();
        }

        public Object getItem(int i) {
            return this.ˏ == null ? null : this.ˏ.getItem(i);
        }

        public long getItemId(int i) {
            return this.ˏ == null ? -1 : this.ˏ.getItemId(i);
        }

        public View getView(int i, View view, ViewGroup viewGroup) {
            return getDropDownView(i, view, viewGroup);
        }

        public View getDropDownView(int i, View view, ViewGroup viewGroup) {
            if (this.ˏ == null) {
                return null;
            }
            return this.ˏ.getDropDownView(i, view, viewGroup);
        }

        public boolean hasStableIds() {
            return this.ˏ != null && this.ˏ.hasStableIds();
        }

        public void registerDataSetObserver(DataSetObserver dataSetObserver) {
            if (this.ˏ != null) {
                this.ˏ.registerDataSetObserver(dataSetObserver);
            }
        }

        public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {
            if (this.ˏ != null) {
                this.ˏ.unregisterDataSetObserver(dataSetObserver);
            }
        }

        public boolean areAllItemsEnabled() {
            ListAdapter listAdapter = this.ˋ;
            if (listAdapter != null) {
                return listAdapter.areAllItemsEnabled();
            }
            return true;
        }

        public boolean isEnabled(int i) {
            ListAdapter listAdapter = this.ˋ;
            if (listAdapter != null) {
                return listAdapter.isEnabled(i);
            }
            return true;
        }

        public int getItemViewType(int i) {
            return 0;
        }

        public int getViewTypeCount() {
            return 1;
        }

        public boolean isEmpty() {
            return getCount() == 0;
        }
    }

    public AppCompatSpinner(Context context) {
        this(context, null);
    }

    public AppCompatSpinner(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, c.ˏˏ);
    }

    public AppCompatSpinner(Context context, AttributeSet attributeSet, int i) {
        this(context, attributeSet, i, -1);
    }

    public AppCompatSpinner(Context context, AttributeSet attributeSet, int i, int i2) {
        this(context, attributeSet, i, i2, null);
    }

    public AppCompatSpinner(Context context, AttributeSet attributeSet, int i, int i2, Theme theme) {
        TypedArray obtainStyledAttributes;
        Throwable e;
        final b bVar;
        cw ॱ;
        CharSequence[] ʻ;
        SpinnerAdapter arrayAdapter;
        super(context, attributeSet, i);
        this.ॱ = new Rect();
        cw ॱ2 = cw.ॱ(context, attributeSet, g.ˌᐝ, i, 0);
        this.ˏ = new ca(this);
        if (theme != null) {
            this.ʻ = new ax(context, theme);
        } else {
            int ॱॱ = ॱ2.ॱॱ(g.ˎˍ, 0);
            if (ॱॱ != 0) {
                this.ʻ = new ax(context, ॱॱ);
            } else {
                this.ʻ = VERSION.SDK_INT < 23 ? context : null;
            }
        }
        if (this.ʻ != null) {
            if (i2 == -1) {
                try {
                    obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, ˋ, i, 0);
                    try {
                        if (obtainStyledAttributes.hasValue(0)) {
                            i2 = obtainStyledAttributes.getInt(0, 0);
                        }
                        if (obtainStyledAttributes != null) {
                            obtainStyledAttributes.recycle();
                        }
                    } catch (Exception e2) {
                        e = e2;
                        try {
                            Log.i("AppCompatSpinner", "Could not read android:spinnerMode", e);
                            if (obtainStyledAttributes != null) {
                                obtainStyledAttributes.recycle();
                            }
                            if (i2 == 1) {
                                bVar = new b(this, this.ʻ, attributeSet, i);
                                ॱ = cw.ॱ(this.ʻ, attributeSet, g.ˌᐝ, i, 0);
                                this.ˎ = ॱ.ʼ(g.ˎـ, -2);
                                bVar.ˊ(ॱ.ˋ(g.ˎꓸ));
                                bVar.ˋ(ॱ2.ˏ(g.ˎˌ));
                                ॱ.ॱ();
                                this.ˊ = bVar;
                                this.ʽ = new cm(this, this) {
                                    final /* synthetic */ AppCompatSpinner ˎ;

                                    public bt ˎ() {
                                        return bVar;
                                    }

                                    public boolean ˏ() {
                                        if (!this.ˎ.ˊ.ˎ()) {
                                            this.ˎ.ˊ.ˊ();
                                        }
                                        return true;
                                    }
                                };
                            }
                            ʻ = ॱ2.ʻ(g.ˎꜟ);
                            if (ʻ != null) {
                                arrayAdapter = new ArrayAdapter(context, 17367048, ʻ);
                                arrayAdapter.setDropDownViewResource(ah$j.ʼॱ);
                                setAdapter(arrayAdapter);
                            }
                            ॱ2.ॱ();
                            this.ᐝ = true;
                            if (this.ʼ != null) {
                                setAdapter(this.ʼ);
                                this.ʼ = null;
                            }
                            this.ˏ.ˋ(attributeSet, i);
                        } catch (Throwable th) {
                            e = th;
                            if (obtainStyledAttributes != null) {
                                obtainStyledAttributes.recycle();
                            }
                            throw e;
                        }
                    }
                } catch (Exception e3) {
                    e = e3;
                    obtainStyledAttributes = null;
                    Log.i("AppCompatSpinner", "Could not read android:spinnerMode", e);
                    if (obtainStyledAttributes != null) {
                        obtainStyledAttributes.recycle();
                    }
                    if (i2 == 1) {
                        bVar = new b(this, this.ʻ, attributeSet, i);
                        ॱ = cw.ॱ(this.ʻ, attributeSet, g.ˌᐝ, i, 0);
                        this.ˎ = ॱ.ʼ(g.ˎـ, -2);
                        bVar.ˊ(ॱ.ˋ(g.ˎꓸ));
                        bVar.ˋ(ॱ2.ˏ(g.ˎˌ));
                        ॱ.ॱ();
                        this.ˊ = bVar;
                        this.ʽ = /* anonymous class already generated */;
                    }
                    ʻ = ॱ2.ʻ(g.ˎꜟ);
                    if (ʻ != null) {
                        arrayAdapter = new ArrayAdapter(context, 17367048, ʻ);
                        arrayAdapter.setDropDownViewResource(ah$j.ʼॱ);
                        setAdapter(arrayAdapter);
                    }
                    ॱ2.ॱ();
                    this.ᐝ = true;
                    if (this.ʼ != null) {
                        setAdapter(this.ʼ);
                        this.ʼ = null;
                    }
                    this.ˏ.ˋ(attributeSet, i);
                } catch (Throwable th2) {
                    e = th2;
                    obtainStyledAttributes = null;
                    if (obtainStyledAttributes != null) {
                        obtainStyledAttributes.recycle();
                    }
                    throw e;
                }
            }
            if (i2 == 1) {
                bVar = new b(this, this.ʻ, attributeSet, i);
                ॱ = cw.ॱ(this.ʻ, attributeSet, g.ˌᐝ, i, 0);
                this.ˎ = ॱ.ʼ(g.ˎـ, -2);
                bVar.ˊ(ॱ.ˋ(g.ˎꓸ));
                bVar.ˋ(ॱ2.ˏ(g.ˎˌ));
                ॱ.ॱ();
                this.ˊ = bVar;
                this.ʽ = /* anonymous class already generated */;
            }
        }
        ʻ = ॱ2.ʻ(g.ˎꜟ);
        if (ʻ != null) {
            arrayAdapter = new ArrayAdapter(context, 17367048, ʻ);
            arrayAdapter.setDropDownViewResource(ah$j.ʼॱ);
            setAdapter(arrayAdapter);
        }
        ॱ2.ॱ();
        this.ᐝ = true;
        if (this.ʼ != null) {
            setAdapter(this.ʼ);
            this.ʼ = null;
        }
        this.ˏ.ˋ(attributeSet, i);
    }

    public Context getPopupContext() {
        if (this.ˊ != null) {
            return this.ʻ;
        }
        if (VERSION.SDK_INT >= 23) {
            return super.getPopupContext();
        }
        return null;
    }

    public void setPopupBackgroundDrawable(Drawable drawable) {
        if (this.ˊ != null) {
            this.ˊ.ˊ(drawable);
        } else if (VERSION.SDK_INT >= 16) {
            super.setPopupBackgroundDrawable(drawable);
        }
    }

    public void setPopupBackgroundResource(@DrawableRes int i) {
        setPopupBackgroundDrawable(ar.ˋ(getPopupContext(), i));
    }

    public Drawable getPopupBackground() {
        if (this.ˊ != null) {
            return this.ˊ.ʼ();
        }
        if (VERSION.SDK_INT >= 16) {
            return super.getPopupBackground();
        }
        return null;
    }

    public void setDropDownVerticalOffset(int i) {
        if (this.ˊ != null) {
            this.ˊ.ˏ(i);
        } else if (VERSION.SDK_INT >= 16) {
            super.setDropDownVerticalOffset(i);
        }
    }

    public int getDropDownVerticalOffset() {
        if (this.ˊ != null) {
            return this.ˊ.ˏॱ();
        }
        if (VERSION.SDK_INT >= 16) {
            return super.getDropDownVerticalOffset();
        }
        return 0;
    }

    public void setDropDownHorizontalOffset(int i) {
        if (this.ˊ != null) {
            this.ˊ.ˎ(i);
        } else if (VERSION.SDK_INT >= 16) {
            super.setDropDownHorizontalOffset(i);
        }
    }

    public int getDropDownHorizontalOffset() {
        if (this.ˊ != null) {
            return this.ˊ.ʽ();
        }
        if (VERSION.SDK_INT >= 16) {
            return super.getDropDownHorizontalOffset();
        }
        return 0;
    }

    public void setDropDownWidth(int i) {
        if (this.ˊ != null) {
            this.ˎ = i;
        } else if (VERSION.SDK_INT >= 16) {
            super.setDropDownWidth(i);
        }
    }

    public int getDropDownWidth() {
        if (this.ˊ != null) {
            return this.ˎ;
        }
        if (VERSION.SDK_INT >= 16) {
            return super.getDropDownWidth();
        }
        return 0;
    }

    public void setAdapter(SpinnerAdapter spinnerAdapter) {
        if (this.ᐝ) {
            super.setAdapter(spinnerAdapter);
            if (this.ˊ != null) {
                this.ˊ.ˏ(new e(spinnerAdapter, (this.ʻ == null ? getContext() : this.ʻ).getTheme()));
                return;
            }
            return;
        }
        this.ʼ = spinnerAdapter;
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (this.ˊ != null && this.ˊ.ˎ()) {
            this.ˊ.ˋ();
        }
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (this.ʽ == null || !this.ʽ.onTouch(this, motionEvent)) {
            return super.onTouchEvent(motionEvent);
        }
        return true;
    }

    protected void onMeasure(int i, int i2) {
        super.onMeasure(i, i2);
        if (this.ˊ != null && MeasureSpec.getMode(i) == Integer.MIN_VALUE) {
            setMeasuredDimension(Math.min(Math.max(getMeasuredWidth(), ˎ(getAdapter(), getBackground())), MeasureSpec.getSize(i)), getMeasuredHeight());
        }
    }

    public boolean performClick() {
        if (this.ˊ == null) {
            return super.performClick();
        }
        if (!this.ˊ.ˎ()) {
            this.ˊ.ˊ();
        }
        return true;
    }

    public void setPrompt(CharSequence charSequence) {
        if (this.ˊ != null) {
            this.ˊ.ˋ(charSequence);
        } else {
            super.setPrompt(charSequence);
        }
    }

    public CharSequence getPrompt() {
        return this.ˊ != null ? this.ˊ.ˏ() : super.getPrompt();
    }

    public void setBackgroundResource(@DrawableRes int i) {
        super.setBackgroundResource(i);
        if (this.ˏ != null) {
            this.ˏ.ˎ(i);
        }
    }

    public void setBackgroundDrawable(Drawable drawable) {
        super.setBackgroundDrawable(drawable);
        if (this.ˏ != null) {
            this.ˏ.ˋ(drawable);
        }
    }

    @RestrictTo({d.LIBRARY_GROUP})
    public void setSupportBackgroundTintList(@Nullable ColorStateList colorStateList) {
        if (this.ˏ != null) {
            this.ˏ.ˊ(colorStateList);
        }
    }

    @Nullable
    @RestrictTo({d.LIBRARY_GROUP})
    public ColorStateList ˊ() {
        return this.ˏ != null ? this.ˏ.ˎ() : null;
    }

    @RestrictTo({d.LIBRARY_GROUP})
    public void setSupportBackgroundTintMode(@Nullable Mode mode) {
        if (this.ˏ != null) {
            this.ˏ.ॱ(mode);
        }
    }

    @Nullable
    @RestrictTo({d.LIBRARY_GROUP})
    public Mode ॱ() {
        return this.ˏ != null ? this.ˏ.ॱ() : null;
    }

    protected void drawableStateChanged() {
        super.drawableStateChanged();
        if (this.ˏ != null) {
            this.ˏ.ˋ();
        }
    }

    int ˎ(SpinnerAdapter spinnerAdapter, Drawable drawable) {
        if (spinnerAdapter == null) {
            return 0;
        }
        int makeMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), 0);
        int makeMeasureSpec2 = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), 0);
        int max = Math.max(0, getSelectedItemPosition());
        int min = Math.min(spinnerAdapter.getCount(), max + 15);
        int max2 = Math.max(0, max - (15 - (min - max)));
        View view = null;
        int i = 0;
        max = 0;
        while (max2 < min) {
            View view2;
            int itemViewType = spinnerAdapter.getItemViewType(max2);
            if (itemViewType != max) {
                view2 = null;
            } else {
                itemViewType = max;
                view2 = view;
            }
            view = spinnerAdapter.getView(max2, view2, this);
            if (view.getLayoutParams() == null) {
                view.setLayoutParams(new LayoutParams(-2, -2));
            }
            view.measure(makeMeasureSpec, makeMeasureSpec2);
            i = Math.max(i, view.getMeasuredWidth());
            max2++;
            max = itemViewType;
        }
        if (drawable == null) {
            return i;
        }
        drawable.getPadding(this.ॱ);
        return (this.ॱ.left + this.ॱ.right) + i;
    }
}
